//
//  FSMine2ViewController.m
//  fanshu
//
//  Created by jklin on 16/3/30.
//  Copyright © 2016年 FS. All rights reserved.
//

#import "FSMineViewController.h"
#import "FSUserLoginViewController.h"
#import "FSMineArticleViewController.h"

#import "FSTableViewGroupItem.h"
#import "FSBasicRowItem.h"
#import "FSMineViewController.h"
#import "FSArrowRowItem.h"
#import "FSSwitchRowItem.h"
#import "FSUser.h"

#import "MJExtension.h"
#import "SDImageCache.h"
#import "SDWebImageDownloader.h"
#import "SDWebImageManager.h"
@interface FSMineViewController ()

@property (nonatomic , strong) FSTableViewGroupItem *groupItem;

@property (nonatomic , strong) NSMutableArray *myGroupArray;
@property (nonatomic , strong) FSUser *user;
@end

@implementation FSMineViewController

-(instancetype)init{
    
    UIStoryboard *discoverStoryboard = [UIStoryboard storyboardWithName:@"FSMineViewController" bundle:nil];
    
    self = [discoverStoryboard instantiateInitialViewController];
    
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //判断加载的文件
self.title = @"我的";
    
    //属性设置
    self.tableView.bounces = NO;
    self.tableView.backgroundColor = [UIColor grayColor];
    BOOL islogin = [[NSUserDefaults standardUserDefaults] boolForKey:@"login"];
    if(islogin)
    {
    //登录状态直接从配置文件加载
        NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
        NSString *fileName = [filePath stringByAppendingPathComponent:@"userData.plist"];
        NSData *data = [NSData dataWithContentsOfFile:fileName];
        NSKeyedUnarchiver *unarchiver = [NSKeyedUnarchiver unarchiveObjectWithData:data];
        FSUser *user = [unarchiver decodeObjectForKey:@"userData"];
        self.user = user;
        [self addGroupLogin];
        [self addGroupUserDate];
    
    }else {
        [self addGroupNoLogin];
        [self addGroupUserDate];
    }

}
-(void)addGroupNoLogin{
    FSArrowRowItem *item = [FSArrowRowItem tableViewRowItemWithImage:[UIImage imageNamed:@"Wechat_Session"] title:@"登录/注册" detailtitle:nil];
    item.isLoginItem =YES;
    //设置动作,跳转至注册界面
    item.pushVCClass = [FSUserLoginViewController class];
    
    NSArray *array = [NSArray arrayWithObject:item];
    FSTableViewGroupItem *groupItem = [FSTableViewGroupItem tableViewGroupItemWithRowArray:array headTitle:nil footTitle:nil];
    [self.GroupArray addObject:groupItem];

}
-(void)addGroupLogin{
    //网络加载登录数据
    NSString *pictureURLStr = [NSString stringWithFormat:@"%@",self.user.userIcon];
    
    NSURL * pictureURL = [NSURL URLWithString:pictureURLStr];
    
    [[SDWebImageManager sharedManager]downloadImageWithURL:pictureURL options:kNilOptions progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
        if (error == nil) {
            
            FSArrowRowItem *item = [FSArrowRowItem tableViewRowItemWithImage:image title:self.user.userName detailtitle:nil];
            item.isLoginItem =YES;
            NSArray *array = [NSArray arrayWithObject:item];
            FSTableViewGroupItem *groupItem = [FSTableViewGroupItem tableViewGroupItemWithRowArray:array headTitle:nil footTitle:nil];
            [self.GroupArray addObject:groupItem];
        }
        
    }];


}
-(void)addGroupUserDate{
    FSArrowRowItem *item1 = [FSArrowRowItem tableViewRowItemWithImage:[UIImage imageNamed:@"icon_mine_article_public"] title:@"公开文章" detailtitle:@"0"];
    
        FSArrowRowItem *item2 = [FSArrowRowItem tableViewRowItemWithImage:[UIImage imageNamed:@"icon_mine_article_secret"] title:@"私密文章" detailtitle:@"0"];
    
        FSArrowRowItem *item3 = [FSArrowRowItem tableViewRowItemWithImage:[UIImage imageNamed:@"icon_mine_article_mark"] title:@"我的收藏" detailtitle:@"0"];
    
        FSArrowRowItem *item4 = [FSArrowRowItem tableViewRowItemWithImage:[UIImage imageNamed:@"icon_mine_article_like"] title:@"喜欢的文章" detailtitle:@"0"];
    
        FSArrowRowItem *item5 = [FSArrowRowItem tableViewRowItemWithImage:[UIImage imageNamed:@"icon_personal_wallet"] title:@"我的钱包" detailtitle:nil];
    NSArray *array = @[item1,item2,item3,item4,item5];
    
    FSTableViewGroupItem *groupItem = [FSTableViewGroupItem tableViewGroupItemWithRowArray:array headTitle:nil footTitle:nil];

    [self.GroupArray addObject:groupItem];
    

}
-(void)addGroupDefault{
    FSSwitchRowItem *item1 = [FSSwitchRowItem tableViewRowItemWithImageName:@"icon_mine_night" title:@"夜间模式" detailtitle:nil];
    FSArrowRowItem *item2 = [FSArrowRowItem tableViewRowItemWithImageName:@"icon_request_settings" title:@"通用设置" detailtitle:nil];
    //通用设置跳转界面
    
    FSArrowRowItem *item3 = [FSArrowRowItem tableViewRowItemWithImageName:@"icon_tabbar_share" title:@"分享简书" detailtitle:nil];
    
    FSArrowRowItem *item4 = [FSArrowRowItem tableViewRowItemWithImageName:@"icon_tabbar_share" title:@"意见反馈" detailtitle:nil];
    FSArrowRowItem *item5 = [FSArrowRowItem tableViewRowItemWithImageName:@"icon_tabbar_share" title:@"觉得不错,给个好评吧!" detailtitle:nil];
    
    NSArray *array = @[item1,item2,item3,item4,item5];
    FSTableViewGroupItem *groupItem = [FSTableViewGroupItem tableViewGroupItemWithRowArray:array headTitle:nil footTitle:nil];
    [self.GroupArray addObject:groupItem];
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
